<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「macOS」Crontab定时任务 | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[macOS]Crontab定时任务/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「macOS」Crontab定时任务</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/macOS/">macOS</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <p>我们可以使用crontab执行一些周期任务</p>
<h2 id="0x01-crontab命令"><a href="/1970/01/01/[macOS]Crontab定时任务/#0x01-crontab命令" class="headerlink" title="0x01 crontab命令"></a>0x01 crontab命令</h2><p>crontab [-u user] file</p>
<p>crontab [-u user] { -e | -l | -r }</p>
<ul>
<li><code>crontab file_name</code></li>
</ul>
<p>将file做为crontab的任务列表文件并载入crontab</p>
<ul>
<li><code>crontab -e</code></li>
</ul>
<p>编辑crontab文件内容</p>
<ul>
<li><code>crontab -l</code></li>
</ul>
<p>显示crontab文件内容</p>
<ul>
<li><code>crontab -r</code></li>
</ul>
<p>删除载入后的crontab文件内容</p>
<h2 id="0x02-crontab文件"><a href="/1970/01/01/[macOS]Crontab定时任务/#0x02-crontab文件" class="headerlink" title="0x02 crontab文件"></a>0x02 crontab文件</h2><ul>
<li><p><code>分</code> <code>时</code> <code>日</code> <code>月</code> <code>星期</code> <code>要运行的命令</code></p>
<ul>
<li>第1列 分钟1～59</li>
<li>第2列 小时1～23（0表示子夜）</li>
<li>第3列 日1～31</li>
<li>第4列 月1～12</li>
<li>第5列 星期0～7（0,7表示星期天）</li>
<li>第6列 要运行的命令</li>
</ul>
</li>
<li><p>几个特殊符号：<code>/</code> <code>-</code> <code>,</code></p>
<ul>
<li><code>/</code> 每(per): <code>*/2</code> 每两个单位</li>
<li><code>-</code> 连续: <code>1-5</code> 1,2,3,4,5</li>
<li><code>,</code> 非连续: <code>1,2,5</code> 1和2和5</li>
</ul>
</li>
</ul>
<h2 id="0x03-Demo"><a href="/1970/01/01/[macOS]Crontab定时任务/#0x03-Demo" class="headerlink" title="0x03 Demo"></a>0x03 Demo</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"># 每分钟向用户目录下的log.txt写入当时日期</div><div class="line">* * * * * /bin/date &gt;&gt; ~/log.txt </div><div class="line"></div><div class="line"># 每分钟</div><div class="line">* * * * * /bin/date &gt;&gt; ~/log.txt</div><div class="line"></div><div class="line"># 每5分钟</div><div class="line">*/5 * * * * /bin/date &gt;&gt; ~/log.txt</div><div class="line"></div><div class="line"># 每小时的第五分钟</div><div class="line">5 * * * * /bin/date &gt;&gt; ~/log.txt </div><div class="line"></div><div class="line"># 4月1日早上8点</div><div class="line">0 8 1 4 * /bin/date &gt;&gt; ~/log.txt </div><div class="line"></div><div class="line"># 4和5月 1日早上8点</div><div class="line">0 8 1 4,5 * /bin/date &gt;&gt; ~/log.txt </div><div class="line"></div><div class="line"># 12306</div><div class="line">23-7 * * * * /bin/date &gt;&gt; ~/log.txt</div></pre></td></tr></table></figure>
<h1 id="更新-2015-03-26"><a href="/1970/01/01/[macOS]Crontab定时任务/#更新-2015-03-26" class="headerlink" title="更新(2015-03-26)"></a>更新(2015-03-26)</h1><h2 id="0x01-crontab辅助工具"><a href="/1970/01/01/[macOS]Crontab定时任务/#0x01-crontab辅助工具" class="headerlink" title="0x01 crontab辅助工具"></a>0x01 crontab辅助工具</h2><p>这个工具可以生成crontab文件。比如我们按照自己的想法选择了任务的时间，可以使用它帮助我们生成对应的crontab文件。</p>
<p>地址：<a href="http://www.crontab-generator.org/" target="_blank" rel="external">http://www.crontab-generator.org/</a></p>
<h2 id="0x02-crontab的调试"><a href="/1970/01/01/[macOS]Crontab定时任务/#0x02-crontab的调试" class="headerlink" title="0x02 crontab的调试"></a>0x02 crontab的调试</h2><p>这个建议在命令的后面加上 <code>... &gt;&gt; ~/log.txt 2&gt;&amp;1</code>，这样我们就可以在用户目录下看到crontab执行的日志了。如：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">* * * * * /bin/date &gt;&gt; ~/log.txt &gt;&gt; ~/log.txt 2&gt;&amp;1</div></pre></td></tr></table></figure>
<h2 id="0x03-环境变量"><a href="/1970/01/01/[macOS]Crontab定时任务/#0x03-环境变量" class="headerlink" title="0x03 环境变量"></a>0x03 环境变量</h2><p>虽然我们知道crontab执行时是没有环境变量的，但是这个确是个讨厌的东西。我们在本地调试好了相关的代码，用crontab却怎么执行也不正确，比如执行下面的shell：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">#!/bin/bash</div><div class="line">py3=$(which python3)</div><div class="line">echo py3</div><div class="line">...</div></pre></td></tr></table></figure>
<p>默认情况什么也不会输出。下面的逻辑自然就是错的了。为了解决这个问题，我们可以在最开始加上这么一行 <code>source /etc/profile</code> ，然后在执行就可以顺利拿到我们的值了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">#!/bin/bash</div><div class="line">source /etc/profile</div><div class="line">py3=$(which python3)</div><div class="line">echo py3</div><div class="line">...</div></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/ttdevs/ttdevs.github.io/common/images/logo.png" alt="Create by ttdevs"></p>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Wechat]微信公众号开发流程/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Wechat」微信公众号开发流程</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[macOS]Mac一句话分享/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「macOS」Mac一句话分享</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
